﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using MySql.Data.MySqlClient;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.Models.JoinParty;
using MySqlHelper = ZHDJ.Data.MySqlHelper;
using ZHDJ.Data.BLL;
using ZHDJ.Core;
using ZHDJ.Data.Models.Org;

namespace ZHDJ.JoinParty.BLL
{
    public class ProcesUtil
    {

        #region 步聚常量
        #region 了解入党情况
        /// <summary>
        /// 1、入党申请
        /// </summary>
        public const string JoinStep1 = "JoinStep1";
        /// <summary>
        /// 2、登记备案
        /// </summary>
        public const string JoinStep2 = "JoinStep2";
        #endregion

        #region 入党积极分子的确定和培养教育

        #endregion
        /// <summary>
        /// 3、推荐入党积极份子（会议）
        /// </summary>
        public const string JoinStep3 = "JoinStep3";
        /// <summary>
        /// 4、入党积极份子公示
        /// </summary>
        public const string JoinStep4 = "JoinStep4";
        /// <summary>
        /// 5、确认入党积极份子 
        /// </summary>
        public const string JoinStep5 = "JoinStep5";
        /// <summary>
        /// 6、入党积极份子报上级党委审查备案
        /// </summary>
        public const string JoinStep6 = "JoinStep6";
        /// <summary>
        /// 7、指定培养联系人
        /// </summary>
        public const string JoinStep7 = "JoinStep7";
        /// <summary>
        /// 8、填写入党积极分子登记表
        /// </summary>
        public const string JoinStep8 = "JoinStep8";
        /// <summary>
        /// 9、入党积极份子培养和考察
        /// </summary>
        public const string JoinStep9 = "JoinStep9";
        /// <summary>
        /// 10、入党积极份子培训
        /// </summary>
        public const string JoinStep10 = "JoinStep10";
        /// <summary>
        /// 11、推荐党员发展对象（会议）
        /// </summary>
        public const string JoinStep11 = "JoinStep11";
        /// <summary>
        /// 12、发展对象公示
        /// </summary>
        public const string JoinStep12 = "JoinStep12";
        /// <summary>
        /// 13、确定发展对象
        /// </summary>
        public const string JoinStep13 = "JoinStep13";
        /// <summary>
        /// 14、报上级党委审查备案
        /// </summary>
        public const string JoinStep14 = "JoinStep14";
        /// <summary>
        /// 15、政治审查
        /// </summary>
        public const string JoinStep15 = "JoinStep15";
        /// <summary>
        /// 16、计划生育及社会治安等审查
        /// </summary>
        public const string JoinStep16 = "JoinStep16";
        /// <summary>
        /// 17、发展对象集中培训
        /// </summary>
        public const string JoinStep17 = "JoinStep17";
        /// <summary>
        /// 18、确定入党介绍人
        /// </summary>
        public const string JoinStep18 = "JoinStep18";
        /// <summary>
        /// 19、填写入党志愿书
        /// </summary>
        public const string JoinStep19 = "JoinStep19";
        /// <summary>
        /// 20、支部委员会审查
        /// </summary>
        public const string JoinStep20 = "JoinStep20";
        /// <summary>
        /// 21、基层党委预审
        /// </summary>
        public const string JoinStep21 = "JoinStep21";
        /// <summary>
        /// 22、支部大会讨论接收预备党员（会议）
        /// </summary>
        public const string JoinStep22 = "JoinStep22";
        /// <summary>
        /// 23、上报审批材料（共9份）
        /// </summary>
        public const string JoinStep23 = "JoinStep23";
        /// <summary>
        /// 24、预备党员公示
        /// </summary>
        public const string JoinStep24 = "JoinStep24";
        /// <summary>
        /// 25、组织员谈话
        /// </summary>
        public const string JoinStep25 = "JoinStep25";
        /// <summary>
        /// 26、审批预备党员(会议)
        /// </summary>
        public const string JoinStep26 = "JoinStep26";
        /// <summary>
        /// 27、下发通知
        /// </summary>
        public const string JoinStep27 = "JoinStep27";
        /// <summary>
        /// 28、公布预备党员并编入党组织
        /// </summary>
        public const string JoinStep28 = "JoinStep28";
        /// <summary>
        /// 29、组织预备党员宣誓
        /// </summary>
        public const string JoinStep29 = "JoinStep29";
        /// <summary>
        /// 30、填写预备党员考察表
        /// </summary>
        public const string JoinStep30 = "JoinStep30";
        /// <summary>
        /// 31、预备党员教育考察
        /// </summary>
        public const string JoinStep31 = "JoinStep31";
        /// <summary>
        /// 32、提出书面转正申请
        /// </summary>
        public const string JoinStep32 = "JoinStep32";
        /// <summary>
        /// 33、党小组意见
        /// </summary>
        public const string JoinStep33 = "JoinStep33";
        /// <summary>
        /// 34、党支部意见
        /// </summary>
        //public const string JoinStep34 = "JoinStep34";
        /// <summary>
        /// 35、支部大会讨论转正（会议）
        /// </summary>
        public const string JoinStep35 = "JoinStep35";
        /// <summary>
        /// 36、预备党员转正公示
        /// </summary>
        public const string JoinStep36 = "JoinStep36";
        /// <summary>
        /// 37、上报审批材料（共13份材料）
        /// </summary>
        public const string JoinStep37 = "JoinStep37";
        /// <summary>
        /// 38、党委审批（3个月内）及批复
        /// </summary>
        public const string JoinStep38 = "JoinStep38";
        /// <summary>
        /// 39、谈话和公布预备党员转正
        /// </summary>
        public const string JoinStep39 = "JoinStep39";
        /// <summary>
        /// 40、入党材料归档
        /// </summary>
        public const string JoinStep40 = "JoinStep40";
        /// <summary>
        /// 公共
        /// </summary>
        public const string JoinStepCommon = "JoinStepCommon";
        #endregion

        #region 党委审核步骤
        /// <summary>
        /// 党委审核步骤
        /// </summary>
        public static List<string> PartyCommitteeAuditSteps
        {
            get
            {
                return new List<string>() {
                    ProcesUtil.JoinStep6,
                    ProcesUtil.JoinStep14,
                    ProcesUtil.JoinStep21,
                    ProcesUtil.JoinStep23,
                    ProcesUtil.JoinStep25,
                    ProcesUtil.JoinStep26,
                    ProcesUtil.JoinStep38
                };
            }
        }

        public static string GetPartyCommitteeAuditSteps()
        {
            var steps = string.Join("','", PartyCommitteeAuditSteps.ToArray());
            return string.Format("'{0}'", steps);
        }
        #endregion

        #region 流程操作
        /// <summary>
        ///  入党申请提交时调用 第一步
        /// </summary>
        /// <param name="appllicationID">申请ID</param>
        /// <param name="BranchID">组织id(用于查找处理人)</param>
        /// <returns></returns>
        public static bool CreateProcesStep(string appllicationID, string branchID, string loginID, string userName)
        {
            using (var DB = (new DBInstance()).DB)
            {
                //第一步，申请  默认为通过
                PartyJoinLinkExample mode = new PartyJoinLinkExample();
                mode.StandID = StringHelper.NewID;
                PartyJoinProcess process = new PartyJoinProcess();
                DataSet dsone = ProcesUtilHelp.GetProcessSet(ProcesUtil.JoinStep1);
                mode.CurrentStepID = ProcesUtil.JoinStep1;
                mode.CurrentStepName = dsone.Tables[0].Rows[0]["ProcessName"].ToString();
                mode.Status = (int)Enum_FlowStanceStatus.通过;
                mode.IsEffective = true;
                mode.AppllicationID = appllicationID;
                mode.ToDoHandleUserID = "";
                mode.ToDoHandleUserName = "";

                mode.HandleUserID = loginID;
                mode.HandleUserName = userName;
                mode.HandleTime = DateTime.Now;
                mode.OrderNo = 0;
                mode.CreateTime = DateTime.Now;
                DB.PartyJoinLinkExamples.Add(mode);
                //DB.SaveChanges();
                if (DB.SaveChanges() < 0)
                {
                    ZHDJ.Core.Log.Log4netHelper.WriteLog(new Exception(string.Format("第一步创建失败：AppllicationID：{0}", appllicationID)));
                    return false;
                }


                //申请成功发送短息给申请人
                var om = DB.PartyOrgMembers.Find(loginID);
                string msgContent = string.Format("{0}同志，您的入党申请已经提交成功，办理状态可登录智慧党建平台查看！",userName);
                SMSHelper.CreateSmsSendRecord(om.MobileNo, msgContent, "入党申请提交");

                //第二步，创建流程  
                PartyJoinLinkExample mode2 = new PartyJoinLinkExample();
                mode2.StandID = StringHelper.NewID;
                PartyJoinProcess process2 = new PartyJoinProcess();
                DataSet dstwo = ProcesUtilHelp.GetProcessSet(ProcesUtil.JoinStep2);
                mode2.CurrentStepID = ProcesUtil.JoinStep2;
                mode2.CurrentStepName = dstwo.Tables[0].Rows[0]["ProcessName"].ToString();
                mode2.Status = (int)Enum_FlowStanceStatus.未处理;
                mode2.IsEffective = true;
                mode2.AppllicationID = appllicationID;
                mode2.ToDoHandleUserID = branchID;
                mode2.ToDoHandleUserName = branchID;
                mode2.OrderNo = 1;
                mode2.CreateTime = DateTime.Now;
                mode2.PreStepID = mode.StandID;
                DB.PartyJoinLinkExamples.Add(mode2);
                var rs = DB.SaveChanges() > 0;
                if (rs)
                {
                    //处理特殊业务(发送待办)
                    DealStepBusiness(mode2, branchID);

                    //发送短信给代办人
                    SendMassageToToDo(mode2.StandID, appllicationID, mode2.CurrentStepName);
                }
                else
                {
                    ZHDJ.Core.Log.Log4netHelper.WriteLog(string.Format("第二步创建失败：AppllicationID：{0}", appllicationID));
                }



                //发送短信
                //if (process2.IsSendMassage == true)
                //{
                //    try
                //    {
                //        SendMassageToToDo(mode2.StandID, mode.AppllicationID, mode2.CurrentStepName);
                //    }
                //    catch (Exception ex)
                //    {
                //        ExceptionLogs.Write(ex);
                //    }
                //}
                return rs;
            }
        }

        /// <summary>
        /// 流程审批时调用
        /// </summary>
        /// <param name="mode">当前流程实例</param>
        /// <param name="branchID">组织id(用于查找处理人)</param>
        /// <returns></returns>
        public static bool ApprovalProcesStep(PartyJoinLinkExample mode, out string ISHandleMessage,
            string branchID = "", string BackStep = "", string MssContent = "")
        {
            using (var DB = (new DBInstance()).DB)
            {
                #region 判断

                ISHandleMessage = "";
                //实时取回当前审核环节的数据

                PartyJoinLinkExample partyJoinLinkExample = DB.PartyJoinLinkExamples.Find(mode.StandID);

                //var Example = new PartyJoinLinkExample();
                //DataSet dsLinkExampleData = ProcesUtilHelp.GetLinkExampleData(mode.ID);
                //Example.Load();
                if (Convert.ToInt32(partyJoinLinkExample.Status.ToString()) != (int)Enum_FlowStanceStatus.未处理)
                {
                    //已经通过的或者取消的不处理
                    ISHandleMessage = "该申请单流程已处理！";
                    return false;
                }
                if (mode.Status == (int)Enum_FlowStanceStatus.未处理)
                {
                    return false;
                }

                #endregion

                //更新当前流程环节状态
                if (!ProcesUtilHelp.UpdatePartyJoinLinkExample(mode))
                {
                    return false;
                }

                //取消当前待办
                //SXT.Party.Utility.PartyTask.TaskDone(mode.StandID, SXT.Utility.SSOClient.CurrentUser.LoginID);


                //创建下一步流程步骤
                PartyJoinLinkExample next = new PartyJoinLinkExample();
                PartyJoinProcess process = new PartyJoinProcess();
                if (mode.Status == (int)Enum_FlowStanceStatus.取消)
                {

                    return ProcesUtilHelp.CancelOrCompleteJoin(mode.AppllicationID, (int)Enum_ApplyStatus.申请取消);
                }
                else if (mode.Status == (int)Enum_FlowStanceStatus.通过)
                {
                    //下一步
                    process = ProcesUtilHelp.GetNextProcess(mode.CurrentStepID);
                    if (process == null) //下一步等于null表示当前为最后一步
                    {
                        ProcesUtilHelp.CancelOrCompleteJoin(mode.AppllicationID, (int)Enum_ApplyStatus.申请成功, true);


                        //发送申请成功短信
                        var pja = DB.PartyJoinApplycation.Find(mode.AppllicationID);
                        string MsgContent = string.Empty;
                        MsgContent = string.Format("{0}您好，您已完成智慧党建系统全部的入党流程，欢迎登录山海天党建信息平台！", pja.UserName);
                        SendMassageToAppllication(mode.AppllicationID, "", MsgContent);
                    }
                }
                else if (mode.Status == (int)Enum_FlowStanceStatus.退回)
                {
                    //退回

                    //第34环节增加自定义退回 陈泳江 201504251525
                    //if (mode.CurrentStepID == ProcesUtil.JoinStep24)
                    //{
                    //    process = ProcesUtilHelp.GetCustomBackProcess(BackStep);
                    //}
                    //else
                    //{
                    process = ProcesUtilHelp.GetBackProcess(mode.CurrentStepID);
                    //}
                    //如果退回到第一步（入党申请步聚）则取消整个申请流程
                    if (process.ID == ProcesUtil.JoinStep1)
                    {
                        ProcesUtilHelp.UpdatePartyJoinApplycation(mode.AppllicationID, (int)Enum_ApplyStatus.申请取消);
                        return true;
                    }
                }
                //找不到下一步
                if (process == null)
                {
                    return false;
                }
                next.CurrentStepID = process.ID;
                next.CurrentStepName = process.ProcessName;
                next.AppllicationID = mode.AppllicationID;
                next.IsEffective = true;
                next.OrderNo = ProcesUtilHelp.GetMaxOrderNo(mode.AppllicationID) + 1;
                next.StandID = StringHelper.NewID;
                next.Status = (int)Enum_FlowStanceStatus.未处理;
                next.PreStepID = mode.StandID;
                next.CreateTime = DateTime.Now;

                HttpContext.Current.Session["NextStandID"] = next.StandID;//缓存下一步流程实例ID

                //待处理人
                var TodoBranchID = branchID;//审核组织
                if (!string.IsNullOrEmpty(TodoBranchID))
                {
                    next.ToDoHandleUserID = TodoBranchID;
                    next.ToDoHandleUserName = TodoBranchID;
                }
                else
                {
                    var app = ZHDJ.WebPage.ControlBusiness.DB.PartyJoinApplycation.Find(mode.AppllicationID);
                    TodoBranchID = app.BranchID;
                    next.ToDoHandleUserID = TodoBranchID;
                    next.ToDoHandleUserName = TodoBranchID;
                }
                DB.PartyJoinLinkExamples.Add(next);
                var rs = DB.SaveChanges() > 0;


                #region 申请人需要填写内容短信通知

                if (mode.CurrentStepID == JoinStep6 || mode.CurrentStepID == JoinStep7 ||
                    mode.CurrentStepID == JoinStep8 || mode.CurrentStepID == JoinStep9 ||
                    mode.CurrentStepID == JoinStep16 || mode.CurrentStepID == JoinStep28 ||
                    mode.CurrentStepID == JoinStep29 || mode.CurrentStepID == JoinStep30)
                {
                    //发送短信
                    //SendMassageToAppllication(mode.AppllicationID, next.CurrentStepName);
                }
                //24步 组织宣誓添加自定义短信功能
                //if (mode.CurrentStepID == JoinStep28)
                //{
                //    //SendMassageToAppllication(mode.AppllicationID, mode.CurrentStepName, MssContent);
                //}

                #endregion

                #region 根据设置是否给下一步骤处理人发送短信

                if (process.IsSendMassage == true)
                {
                    SendMassageToAppllication(mode.AppllicationID, next.CurrentStepName, MssContent);
                    SendMassageToToDo(next.StandID, mode.AppllicationID, next.CurrentStepName);
                }

                #endregion

                return true;
            }
        }

        #endregion

        #region 其他
        /// <summary>
        /// 获取申请单数据
        /// </summary>
        public static PartyJoinApplycation GetApplycation(string applycationID)
        {
            using (var DB = (new DBInstance()).DB)
            {
                var app = DB.PartyJoinApplycation.Find(applycationID);
                if (app != null)
                {
                    return app;
                }
                else return null;
            }
        }

        /// <summary>
        /// 获取GetDataGrid数据源
        /// </summary>
        /// <returns></returns>
        public static DataSet GetToDoFLowByBranchForUp(string BranchID, bool isJoinPartyAdmin)
        {
            var para = new List<MySqlParameter>
            {
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "BranchID",
                    Value =  BranchID
                }
            };


            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat(@"select data.AppllicationID,data.LoginID,pom.UserName,data.CreateTime,
        (case pom.Gender when 0 then '男' when 1 then '女' else '' end) as Gender,
        pom.IdentityCard,pom.Nation,pom.NativePlace,pom.Educational,pom.MobileNo,
        pob.BranchID,pob.BranchName,fStance.ToDoHandleUserID,
        fStance.CurrentStepID,fStance.CurrentStepName,fStance.StandID 
		from tblPartyJoinChildFlow cf 
        inner join tblPartyJoinLinkExample fStance on fStance.StandID=cf.StandID
		left join tblPartyJoinApplycation data on data.AppllicationID=fStance.AppllicationID 
        left join tblpartyorgmember as pom on data.LoginID=pom.LoginID
        left join tblpartyorgbranch as pob on data.BranchID=pob.BranchID
        where fStance.Status=0 and data.Status=1");
            if (!isJoinPartyAdmin)
                sbSql.AppendFormat(" and cf.ToDoBranchID='{0}' ", BranchID);

            sbSql.AppendFormat(@" and cf.Status=0
union
select * from (
        SELECT data.AppllicationID,data.LoginID,pom.UserName,data.CreateTime,
        (case pom.Gender when 0 then '男' when 1 then '女' else '' end) as Gender,
        pom.IdentityCard,pom.Nation,pom.NativePlace,pom.Educational,pom.MobileNo,
        pob.BranchID,pob.BranchName,fStance.ToDoHandleUserID,
        fStance.CurrentStepID,fStance.CurrentStepName,fStance.StandID 
        from tblPartyJoinApplycation data
        inner join tblPartyJoinLinkExample fStance on data.AppllicationID=fStance.AppllicationID
        left join tblpartyorgmember as pom on data.LoginID=pom.LoginID
        left join tblpartyorgbranch as pob on data.BranchID=pob.BranchID
        where fStance.Status=0 and data.Status=1)
t1 where 1=1 and BranchID in (select childbranchid from tblpartyorgbranchrelation where 1=1");
            if (!isJoinPartyAdmin)
                sbSql.AppendFormat(" and Branchid='{0}'", BranchID);
            sbSql.AppendFormat(@"
) and  not exists (select * from tblPartyJoinChildFlow cf where cf.StandID=t1.StandID and cf.Status=0)
and CurrentStepID in ({0})

", ProcesUtil.GetPartyCommitteeAuditSteps());

            sbSql.Append(" order by BranchName,CurrentStepID,CreateTime desc");

            //cmd.CommandText = sbSql.ToString();
            return MySqlHelper.ExecuteDataSet(sbSql.ToString(), para.ToArray());
        }

        /// <summary>
        /// 获取GetDataGrid数据源
        /// </summary>
        /// <returns></returns>
        public static DataSet GetToDoFLowByBranch(string BranchID, bool isJoinPartyAdmin)
        {
            //var para = new List<MySqlParameter>
            //{
            //    new MySqlParameter()
            //    {
            //        DbType = DbType.String,
            //        ParameterName = "BranchID",
            //        Value =  BranchID
            //    }
            //};

            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat(@"select * from (
        SELECT data.AppllicationID,data.LoginID,pom.UserName,data.CreateTime,
        (case pom.Gender when 0 then '男' when 1 then '女' else '' end) as Gender,
        pom.IdentityCard,pom.Nation,pom.NativePlace,pom.Educational,pom.MobileNo,
        pob.BranchID,pob.BranchName,fStance.ToDoHandleUserID,
        fStance.CurrentStepID,fStance.CurrentStepName,fStance.StandID
        from tblPartyJoinApplycation data
        inner join tblPartyJoinLinkExample fStance on data.AppllicationID=fStance.AppllicationID
        left join tblpartyorgmember as pom on data.LoginID=pom.LoginID
        left join tblpartyorgbranch as pob on data.BranchID=pob.BranchID
        where fStance.Status=0 and data.Status=1) t1 where 1=1");
            if (!isJoinPartyAdmin)
                sbSql.AppendFormat("  and BranchID='{0}'", BranchID);
            sbSql.AppendFormat(@"
        and  not exists (select * from tblPartyJoinChildFlow cf where cf.StandID=t1.StandID and cf.Status=0)
        and CurrentStepID not in ({0})

", ProcesUtil.GetPartyCommitteeAuditSteps());


            sbSql.Append(" order by BranchName,CurrentStepID,CreateTime desc");

            //return MySqlHelper.ExecuteDataSet(sbSql.ToString(), para.ToArray());
            return MySqlHelper.ExecuteDataSet(sbSql.ToString());
        }
        #endregion

        #region 步骤特殊业务定义
        /// <summary>
        /// 执行特殊业务
        /// </summary>
        /// <param name="StepID"></param>
        /// <returns></returns>
        private static bool DealStepBusiness(PartyJoinLinkExample mode, string branchID)
        {
            var dic = SetpDealFunctions;
            if (dic.Keys.Contains(mode.CurrentStepID))
            {
                return dic[mode.CurrentStepID](mode, branchID);
            }
            return false;
        }

        /// <summary>
        /// 步骤特殊业务定义字典
        /// </summary>
        private static Dictionary<string, Func<PartyJoinLinkExample, string, bool>> SetpDealFunctions
        {
            get
            {
                var tempDic = new Dictionary<string, Func<PartyJoinLinkExample, string, bool>>();
                tempDic.Add(ProcesUtil.JoinStep2, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep3, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep4, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep5, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep6, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep7, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep8, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep9, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep10, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep11, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep12, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep13, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep14, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep15, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep16, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep17, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep18, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep19, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep20, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep21, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep22, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep23, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep24, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep25, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep26, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep27, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep28, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep29, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep30, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep31, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep32, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep33, CommonDeal);
                //tempDic.Add(ProcesUtil.JoinStep34, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep35, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep36, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep37, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep38, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep39, CommonDeal);
                tempDic.Add(ProcesUtil.JoinStep40, CommonDeal);
                return tempDic;
            }
        }

        #region 第一步特殊业务处理()
        /// <summary>
        /// 第一步特殊业务处理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private static bool DealJoinStep1(PartyJoinLinkExample mode, string branchID)
        {
            //待办
            AddTask(mode, branchID);
            return false;
        }
        #endregion

        #region 公共特殊业务处理
        /// <summary>
        /// 公共特殊业务处理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private static bool CommonDeal(PartyJoinLinkExample mode, string branchID)
        {
            //待办
            AddTask(mode, branchID);
            return false;
        }
        #endregion



        #region 待办
        /// <summary>
        /// 公共待办
        /// </summary>
        /// <returns></returns>
        private static void AddTask(PartyJoinLinkExample mode, string branchID)
        {
            //var url = DicTaskStepUrl[mode.CurrentStepID] + "&RelationID=" + mode.StandID;
            //AddTask(mode, branchID, url);

        }
        /// <summary>
        /// 公共发待办（发组织）
        /// </summary>
        /// <param name="mode">新步骤</param>
        /// <param name="branchID">待办组织ID</param>
        /// <param name="Url">待办地址</param>
        private static void AddTask(PartyJoinLinkExample mode, string branchID, string Url)
        {
            //组织待办
            //AddTask(mode, branchID);
            //var DB = (new DBInstance()).DB;
            //var app = DB.PartyJoinApplycation.Find(mode.AppllicationID);
            //string appBranchID = "";
            //string appProcessName = "";
            //if (app != null) appBranchID = app.BranchID;

            //var set = DB.PartyJoinProcesss.Find(mode.CurrentStepID);
            //if (set != null) appProcessName = set.ProcessName;
            //PartyTaskHelper.AddTaskToBranch(mode.StandID, "入党申请" + appProcessName, SysEnum.TaskCategory.JoinParty,
            //                ZHDJ.WebPage.CurrentUserManager.CurrentUser.LoginID, new string[] { branchID }, Url, branchID == appBranchID ? "本组织" : "下级组织");

        }
        /// <summary>
        /// 发待办
        /// </summary>
        /// <param name="task">待办实例</param>
        //private static void AddTask(Task task)
        //{
        //    PartyTask.AddTask(task.relationID, task.title, task.category, task.fromUser, task.toUsers.ToArray(), task.taskUrl, task.taskSpecie, task.source);
        //}

        /// <summary>
        /// 初始化待办信息
        /// </summary>
        /// <param name="mode">新的实例</param>
        /// <param name="branchID">待办组织id</param>
        /// <param name="isBranchID">是组织待办还个人待办</param>
        //private static Task InitTask(PartyJoinLinkExample mode, string branchID, bool isBranchID, string url)
        //{
        //    ProcessSet set = new ProcessSet() { ID = mode.CurrentStepID };
        //    set.Load();
        //    PartyJoinApplycation app = GetApplycation(mode.AppllicationID);
        //    Task task = new Task();
        //    task.relationID = mode.StandID;
        //    task.source = branchID == app.BranchID ? "本组织" : "下级组织";
        //    task.taskUrl = url;
        //    task.title = "入党申请--" + set.ProcessName;
        //    task.fromUser = SXT.Utility.SSOClient.CurrentUser.LoginID;
        //    task.category = "入党申请";
        //    if (isBranchID)
        //    {
        //        task.toUsers = new List<string>() { branchID };
        //        task.taskSpecie = PartyTask.TaskSpecie.Branch;
        //    }
        //    else
        //    {
        //        task.toUsers = ModuleUtil.GetBranchAdminToDictionaryEx(branchID, "").Keys.ToList();
        //        task.taskSpecie = PartyTask.TaskSpecie.Member;
        //    }
        //    return task;
        //}

        /// <summary>
        /// 待办地址
        /// </summary>
        private static Dictionary<string, string> DicTaskStepUrl
        {
            get
            {
                //公共待办地址
                var commonUrl = "../../../JoinParty/admin/ToDoFlow/ToDoFlowList.aspx?r=" + DateTime.Now.Ticks + "&BranchCenter=1";
                var tempDic = new Dictionary<string, string>();
                tempDic.Add(ProcesUtil.JoinStep1, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep2, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep3, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep4, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep5, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep6, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep7, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep8, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep9, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep10, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep11, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep12, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep13, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep14, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep15, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep16, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep17, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep18, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep19, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep20, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep21, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep22, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep23, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep24, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep25, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep26, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep27, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep28, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep29, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep30, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep31, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep32, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep33, commonUrl);
                //tempDic.Add(ProcesUtil.JoinStep34, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep35, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep36, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep37, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep38, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep39, commonUrl);
                tempDic.Add(ProcesUtil.JoinStep40, commonUrl);
                return tempDic;
            }
        }
        #endregion
        #endregion

        #region 发送短信给下一步处理者
        /// <summary>
        /// 
        /// </summary>
        /// <param name="StandID">流程实例ID</param>
        /// <param name="AppName">申请人姓名</param>
        /// <param name="StepName">下一步骤名称</param>
        public static void SendMassageToToDo(string StandID, string AppllicationID, String StepName)
        {
            using (var DB = (new DBInstance()).DB)
            {
                var pja = DB.PartyJoinApplycation.Find(AppllicationID);
                var om2 = DB.PartyOrgMembers.Find(pja.LoginID);
                var pjle = DB.PartyJoinLinkExamples.Find(StandID);
                var branch = new PartyOrgBranch();
                if (pja.BranchID != null) {
                    branch = DB.PartyOrgBranchs.Find(pja.BranchID);
                }

                string AppName = om2.UserName;
                var ToDoHandleUserID = "";

                if (PartyCommitteeAuditSteps.Contains(pjle.CurrentStepID))//需要上级审批的步骤
                {
                    var parentBranch = DB.PartyOrgBranchs.Find(branch.ParentBranchID);
                    if (parentBranch != null) {
                        ToDoHandleUserID = parentBranch.BranchID;
                    }
                }
                if (ToDoHandleUserID == "") {
                    ToDoHandleUserID = pjle.ToDoHandleUserID;
                }
                var allLoginID = DB.PartyOrgMembers.Where(o => o.BranchID == ToDoHandleUserID && o.IsPartyWorker == true).ToList();

                //string[] allLoginID = pjle.ToDoHandleUserID.Split(',');

                string MsgContent = "尊敬的{0}同志，现有{3}{1}申请入党，当前步骤已到达{2}，请您登录山海天党建信息平台及时处理。";
                foreach (var item in allLoginID)
                {
                    SMSHelper.CreateSmsSendRecord(item.MobileNo, string.Format(MsgContent, item.UserName, AppName, StepName,branch.BranchName), "入党申请处理");
                }
            }
        }
        #endregion
        #region 发送信息给申请人(需填写内容的环节)

        /// <summary>
        /// 发送信息to申请人main方法
        /// </summary>
        /// <param name="AppllicationID">申请ID</param>
        /// <param name="CurrentStepName">当前环节名称</param>
        public static void SendMassageToAppllication(string AppllicationID, string CurrentStepName, string Content = "")
        {
            using (var DB = (new DBInstance()).DB)
            {
                var pja = DB.PartyJoinApplycation.Find(AppllicationID);
                var om = DB.PartyOrgMembers.Find(pja.LoginID);

                string[] Mobile = new string[] { om.MobileNo };
                string msgContent = string.Format("{0}同志，您的入党申请已经到达【{1}】步骤，请及时查看。", om.UserName, CurrentStepName);
                if (!string.IsNullOrEmpty(Content))
                {
                    msgContent = Content;
                }
                try
                {
                    SMSHelper.CreateSmsSendRecord(om.MobileNo, msgContent, "入党申请处理");
                }
                catch (Exception ex)
                {
                    ZHDJ.Core.Log.Log4netHelper.WriteLog(new Exception(string.Format("SendMassageToAppllication失败，异常信息为：{0}", ex.ToString())));
                }
            }
        }
        #endregion

    }
}
